<template>
  <div>
    <!-- 
          el-dialog
          center	是否对头部和底部采用居中布局	boolean
       -->
    <el-dialog
      center
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isShow"
      :before-close="cancel"
    >
      <el-form :model="cartlist" ref="cateInfo" :rules="rules">
        <el-form-item
          label="上级分类"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-select v-model="cartlist.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in cate"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="cartlist.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth" prop="catename"
        v-if="cartlist.pid != 0">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-change='handleRemove'
            :auto-upload='false'
           :file-list="filelist"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="cartlist.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRoutes } from "../../router/index";
import { addCate, cateinfo,cateedit } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      rules: {
        rolename: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
      cartlist: {
        pid: 0,
        catename: "",
        img: "",
        status: 1,
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
      formLabelWidth: "120px",
      indexRoutes: indexRoutes,
      filelist: []
    };
  },
  computed: {
    ...mapGetters({
      cate: "spec/getcate",
    }),
  },

  mounted() {},
  methods: {
    handleRemove(file) {
      console.log(file)
    this.cartlist.img=file.raw
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
    
    },

    ...mapActions({
      getCate: "spec/getCateList",
    }),
    // 清空表单
    reset() {
      (this.cartlist = {
        pid: "",
        catename: "",
        img: "",
        status: "",
      }),
        this.$refs.cateInfo.resetFields();
         //图片数组清空
      this.filelist = [];
    },
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    // 封装一个事件
    lookup(id) {
      console.log(id, 111111111111111);
      //  获取一条分类数据
      cateinfo( {id} ).then((res) => {
        if(res.code==200){
           console.log(res);
        this.cartlist=res.list
        this.cartlist.id=id
        this.filelist=[{url:this.$img+this.cartlist.img}]
        }else{
           this.$message.error(res.msg);
        }
       
      });
    },
    add() {
      this.$refs.cateInfo.validate((val) => {
        if (val) {
          if (this.isAdd) {
            // 商品添加
            console.log(111111111111222222222);
            addCate(this.cartlist).then((res) => {
              console.log(this.cartlist);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getCate();
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
          
            cateedit(this.cartlist).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);
              
                this.cancel();
               this.getCate()
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
